package com.msb.mall.seckill.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.msb.common.utils.R;
import com.msb.mall.seckill.dto.SeckillSkuRedisDTO;
import com.msb.mall.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService service;

    /**
     * 获取后一天的秒杀商品
     */
    @ResponseBody
    @RequestMapping("/sessionSkus")
    public R getSeckillSkuInfo(){

        List<SeckillSkuRedisDTO> list = service.getSeckillSkuInfo();

        return R.ok().put("data",list);
    }

    /**
     * 秒杀详情
     */
    @ResponseBody
    @RequestMapping("/sessionInfo/{skuId}")
    public R getSeckillSessionBySkuId(@PathVariable("skuId") Long skuId){

        SeckillSkuRedisDTO dto = service.getSeckillSessionBySkuId(skuId);

        return R.ok().put("seckillInfo",dto);
    }

    /**
     * 秒杀商品抢购
     * @param killId 秒杀编号  1_2 活动场次_商品编号
     * @param rangCode 随机码
     * @param count 商品数量
     * @return
     */
    @RequestMapping("/kill")
    public String killProductAddCart(@RequestParam("killId") String killId,
                                @RequestParam("rangCode") String rangCode,
                                @RequestParam("count") Integer count,
                                     Model model)
    {

        String kill = service.kill(killId, rangCode, count);
        model.addAttribute("orderSn",kill);
        return "success";
    }

}
